import { createI18n } from 'vue-i18n'

import Storage from '@/assets/js/storage'

import en from 'element-plus/es/locale/lang/en'
import zh from 'element-plus/es/locale/lang/zh-cn'
import zhtw from 'element-plus/es/locale/lang/zh-tw'

const langFiles = import.meta.glob('./locale/*.json', { eager: true })

//声明一个数据模型，对应i18n中的message属性
const messages = {}

const elementLocale = {
  en: en,
  zh: zh,
  zhtw: zhtw,
}

// 批量导入语言文件
for (const path in langFiles) {
  let langName = path.split('/')[2].split('.')[0] //匹配en|zh这样的值
  // messages[langName] = langFiles[path].default
  messages[langName] = {
    ...langFiles[path].default,  // 直接将语言文案导入
    el: elementLocale[langName],  // 保持Element Plus的语言设置
  }
}

const i18n = createI18n({
  locale: Storage.get('common')?.locale ?? 'zh',
  globalInjection: true,
  fallbackLocale: 'zh',
  messages,
  legacy: false,
})

export default i18n

export function $t(args) {
  return i18n.global.t(args)
}
